<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/10/26
 * Time: 14:28
 */
namespace common\services;

use DateTime;
use DateTimeZone;
use common\services\tool\ToolTime;

class ToolTimeService
{
    /**
     * 时间戳转换时间
     * @param int $unixTimestamp    时间戳
     * @param int $type             类型 0.秒 1.毫秒
     * @return false|string         时间 XXXX-XX-XX XX:XX:XX
     * @return mixed
     */
    public static function timeStampHandle($unixTimestamp = 0, $type = 0)
    {
        $date = date('Y-m-d H:i:s',0);
        switch ($type) {
            case 0 :
                $date = date('Y-m-d H:i:s',$unixTimestamp);
                break;
            case 1 :
                $date = ToolTime::microTimeToStr($unixTimestamp);
                break;
            default :
                break;
        }
        $array['date'] = $date;
        return $array;
    }

    /**
     * 时间转换时间戳
     * @param $date         日期
     * @return false|int    时间戳
     */
    public static function dateHandle($date)
    {
        if (empty($date)) {
            $array['unix_timestamp'] = strtotime(date('Y-m-d H:i:s'));
        } else {
            $array['unix_timestamp'] = strtotime($date);
        }
        return $array;
    }

    /**
     * 时间计算
     * @param $time1
     * @param $time2
     * @return int
     */
    public static function getTimeDifference($time1, $time2)
    {
        $array['second'] = ToolTime::getTimeDifference($time1, $time2);
        $array['day']    = ToolTime::getTimeBetweenDays($time1, $time2);
        return $array;
    }

    /**
     * 计算当前时间距离指定时间差距多少天
     * @param $time
     * @return mixed
     */
    public static function getDayPassed($time)
    {
        $array['second'] = ToolTime::getDayPassed($time);
        return $array;
    }

    /**
     * 日期的增减
     * @param $date 时间
     * @param $day  增加的天数
     * @param $type 减少/增加
     * @return mixed
     */
    public static function getDateAdd($date, $day, $type)
    {
        if ($type == 2) {   // 减少
            $day = '-' . $day;
        }
        $array['date'] = ToolTime::getDateAdd($date, $day);
        return $array;
    }

    /**
     * 时间距离当前时间的描述
     * @param $time
     * @return mixed
     */
    public static function getTimeToStringBetweenDateTimeWithToday($time)
    {
        $array['interval'] = ToolTime::getTimeToStringBetweenDateTimeWithToday($time);
        return $array;
    }

    /**
     * 将N秒转化成其他单位的字符串
     * @param $time
     * @return mixed
     */
    public static function secondsToStr($time)
    {
        $array['interval'] = ToolTime::secondsToStr($time);
        return $array;
    }
}